package cn.itcast.usermanage.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private NewUserMapper userMapper;
	
	@Override
	public EasyUIResult queryUserByPages(Integer page, Integer size) {
		
	//List<User> uList= userMapper.queryUserByPages((page-1)*size,size);
			//Long count = userMapper.queryTotal();
		PageHelper.startPage(page, size);
		User user = new User();
		List<User> uList =  userMapper.select(user);
		PageInfo<User> pageInfo = new PageInfo<User>(uList);
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setRows(pageInfo.getList());
		easyUIResult.setTotal(pageInfo.getTotal());
		return easyUIResult;
	}

	@Override
	public void addUsers(User user1, User user2) {
		this.userMapper.insert(user1);
		// 制造异常
		//int i=1/0;
		this.userMapper.insert(user2);
		
	}

	@Override
	public Boolean addUser(User user) {
		int result = userMapper.insert(user);
		if(result>0){
			return true;
		}
		return false;
	}

	@Override
	public Boolean updateUser(User user) {
		return userMapper.updateByPrimaryKeySelective(user)>0;
	}

	@Override
	public boolean deleteUser(String[] ids) throws Exception {
		Example example = new Example(User.class);
		example.createCriteria().andIn("id", Arrays.asList((Object[])ids));
		return userMapper.deleteByExample(example)>0;
	}

	@Override
	public User queryUserById(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveUser(User user) {
		userMapper.insert(user);
	}

	@Override
	public void deleteUserById(Long[] ids) {
		Example example = new Example(User.class);
		example.createCriteria().andIn("id", Arrays.asList((Object[])ids));
		userMapper.deleteByExample(example);
	}

}
